<template>
    <div class="app-container">
        <div>
            <el-form ref="elForm" :model="form" :rules="rules" size="medium" label-width="80px">
                <el-form-item label="路线名称" prop="routeName">
                    <el-input v-model="form.routeName" placeholder="请输入路线名称" clearable :style="{width: '100%'}">
                    </el-input>
                </el-form-item>
                <el-form-item label="站点名称" prop="stationName">
                    <div v-for="(item,index) in form.stations">
                        <el-input
                            class="routeInput"
                            v-model="item.stationName"
                            placeholder="请输入路线名称"
                            clearable>
                            <el-button
                                type="primary"
                                plain
                                slot="append"
                                icon="el-icon-delete"
                                @click="deleteRoute(index)"
                            >
                            </el-button>
                            <el-button
                                type="primary"
                                plain
                                slot="append"
                                icon="el-icon-top"
                                @click="goTop(index)"
                            >
                            </el-button>
                            <el-button
                                type="primary"
                                plain
                                slot="append"
                                icon="el-icon-bottom"
                                @click="goBottom(index)"
                            >
                            </el-button>
                        </el-input>
                    </div>
                    <div class="addBtn">
                        <el-button size="mini" icon="el-icon-plus" @click="addRoute"></el-button>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="handelConfirm">确定</el-button>
            </div>
        </div>
    </div>
</template>
<script>
    import {addRoute} from "@/api/backstage/route"
    import Vue from "vue";
    import {isBlank} from "../../../utils/StringUtils";

    export default {
        inheritAttrs: false,
        components: {},
        data() {
            return {
                form: {},
                rules: {
                    routeName: [{
                        required: true,
                        message: '请输入路线名称',
                        trigger: 'blur'
                    }],
                },
            }
        },
        methods: {
            goTop(index) {
                if (index !== 0){
                    let tempOption = this.form.stations[index - 1];
                    this.$set(this.form.stations, index - 1, this.form.stations[index]);
                    this.$set(this.form.stations, index, tempOption);
                    this.form.stations.forEach((item,index)=>{
                        this.$set(item,'stationNum',index);
                    })
                }
            },
            goBottom(index){
                if (index !== this.form.stations.length - 1){
                    let tempOption = this.form.stations[index + 1];
                    this.$set(this.form.stations, index + 1, this.form.stations[index]);
                    this.$set(this.form.stations, index, tempOption);
                    this.form.stations.forEach((item,index)=>{
                        this.$set(item,'stationNum',index);
                    })
                }
            },
            deleteRoute(index) {
                this.form.stations.splice(index, 1);
            },
            addRoute() {
                if (this.form.stations === undefined || this.form.stations.length === 0) {
                    Vue.set(this.form,"stations",[{stationName: undefined}])
                } else {
                    this.form.stations.push({stationName: undefined});
                }
            },
            close() {
                this.$emit("onClose");
            },
            handelConfirm() {
                if (isBlank(this.form.routeName)){
                    this.msgError("请输入路线名称");
                    return;
                }
                let params = {
                    routeName: this.form.routeName,
                    stations: this.form.stations
                }
                this.$emit("on-confirm",params);
            },
        }
    }

</script>
<style scoped>
    .footer {
        float: right;
    }

    .addBtn {
        margin-top: 5px;
    }

    .routeInput {
        margin-top: 5px;
    }
</style>
